//
//  BITabBarItemView.m
//  BIProjectModule
//
//  Created by yown on 2022/2/17.
//

#import "BITabBarItemView.h"

#import <BIToolModule/BIProjectDefine.h>
#import <BIToolModule/NSString+Network.h>

#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

@interface BITabBarItemView ()

@property (nonatomic) UILabel *itemTextLabel;
@property (nonatomic) UIImageView *itemImageView;

/// 数据
@property (nonatomic) BITabItemModel *itemModel;
/// 点击回调
@property (nonatomic, copy) BITabBarClickBlock clickBlock;

@end

@implementation BITabBarItemView

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame clickBlock:(BITabBarClickBlock)clickBock {
    self = [super initWithFrame:frame];
    if (self) {
        self.clickBlock = clickBock;
        [self bi_setupView];
        [self bi_setupLayout];
    }
    return self;
}

#pragma mark - Setup

- (void)bi_setupView {
    self.backgroundColor = [UIColor clearColor];
    self.adjustsImageWhenHighlighted = NO;
    self.adjustsImageWhenDisabled = NO;
    [self addTarget:self action:@selector(bi_clickTabBarItem) forControlEvents:UIControlEventTouchUpInside];
    
    self.itemTextLabel = [UILabel new];
    self.itemTextLabel.font = BIFont(11);
    self.itemTextLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.itemTextLabel];
    
    self.itemImageView = [UIImageView new];
    [self addSubview:self.itemImageView];
}

- (void)bi_setupLayout {
    [self.itemTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-3);
        make.height.mas_equalTo(@(14));
    }];
    
    [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@(22));
        make.top.mas_equalTo(self).offset(8);
        make.centerX.mas_equalTo(self);
    }];
}

#pragma mark - Public Method

- (void)refreshTabBarItem:(BITabItemModel *)itemModel {
    self.itemModel = itemModel;
    
    self.itemTextLabel.text = itemModel.itemName;
    self.itemTextLabel.textColor = itemModel.isSelected ? itemModel.itemSelectedColor : itemModel.itemNormalColor;

    self.itemImageView.tintColor = itemModel.isSelected ? itemModel.itemSelectedColor : itemModel.itemNormalColor;
    NSString *imageURL = itemModel.isSelected ? itemModel.itemSelectedImage : itemModel.itemNormalImage;
    
    if ([NSString isURL:imageURL]) {
        [self.itemImageView sd_setImageWithURL:[NSURL URLWithString:imageURL]];
    } else {
        self.itemImageView.image = [UIImage imageNamed:imageURL];
    }
}

#pragma mark - Action

- (void)bi_clickTabBarItem {
    BISafeBlock(self.clickBlock, self.itemModel);
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.3;
    NSMutableArray *values = [NSMutableArray new];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05, 1.05, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.layer addAnimation:animation forKey:nil];
}

@end
